package org.mil.bean.bo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mil.bean.dom.AudioTrackDO;
import org.mil.bean.dom.FileDO;
import org.mil.bean.dom.GeneralTrackDO;
import org.mil.bean.dom.TrackDO;
import org.mil.bean.dom.VideoTrackDO;

public class FileInfo {
	private String sha1;
	private List<Track> tracks;
	private GeneralTrack generalTrack;
	private VideoTrack chiefVideoTrack;
	private AudioTrack chiefAudioTrack;		//主音轨
	private File handler;
	private double fileSize;
	private String fileName;
	public static FileInfo newInstance(FileDO fileDO){
		if (fileDO==null) return null;
		FileInfo fi=new FileInfo();
		fi.load(fileDO);
		return fi;
	}
	public void load(FileDO fileDO){
		if (fileDO==null) return;
		tracks=new ArrayList<Track>();
		List<TrackDO> tdos=fileDO.getTracks();
		 
		
		for(TrackDO tdo:tdos)
			switch (tdo.getType()){
			case TrackDO.T_GENERNAL:
				GeneralTrack gt=TrackFactory.newGeneralTrack();
				gt.load((GeneralTrackDO) tdo);
				tracks.add(gt);
				generalTrack=gt;
				break;
			case TrackDO.T_VIDEO:
				VideoTrack vt=TrackFactory.newVideoTrack();
				vt.load((VideoTrackDO) tdo);
				tracks.add(vt);
				chiefVideoTrack=vt;
				break;
			case TrackDO.T_AUDIO:
				AudioTrack at=TrackFactory.newAudioTrack();
				at.load((AudioTrackDO) tdo);
				tracks.add(at);
				//将码率最大的那条音轨作为主音轨
				if (chiefAudioTrack==null)
					chiefAudioTrack=at;
				else 
				if (chiefAudioTrack.getBit_rate()<at.getBit_rate())
				chiefAudioTrack=at;
				break;
			case TrackDO.T_TEXT:
				TextTrack tt=TrackFactory.newTextTrack();
				//to do
				tracks.add(tt);
				break;
				
			}
	}

	public void setHandler(File handler) {
		if (handler==null) return ;
		this.handler = handler;
		fileSize=handler.length()/1048576.0d;
		fileName=handler.getName();
	}
	public String getSha1() {
		return sha1;
	}

	public List<Track> getTracks() {
		return tracks;
	}

	public GeneralTrack getGeneralTrack() {
		return generalTrack;
	}

	public VideoTrack getChiefVideoTrack() {
		return chiefVideoTrack;
	}

	public AudioTrack getChiefAudioTrack() {
		return chiefAudioTrack;
	}

	public File getHandler() {
		return handler;
	}
	public void setFileSize(double fileSize) {
		this.fileSize = fileSize;
	}
	public double getFileSize() {
		return fileSize;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileName() {
		return fileName;
	}
	

}
